<template>
  <div class="sign-form">
    <el-form
      :disabled="!!signData"
      label-width="120px">
      <div class="sign-form-title">
        <b>商品信息</b>
      </div>
      <div class="m-t-10">
        <div
          class="flex"
          v-for="(item, index) in deliveryForm.goodsInfo"
          :key="index">
          <div class="flex1">
            <el-row>
              <el-col :span="8">
                <el-form-item label="商品名称">
                  <!-- <el-input v-model="item.goodsName"></el-input> -->
                  <el-select
                    clearable
                    @change="goodsChange(index)"
                    v-model="item.goodsName">
                    <el-option
                      v-for="(item, i) in goodsList"
                      :key="i"
                      :value="item.goodsName">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="产地">
                  <el-cascader
                    v-model="item.originPlace"
                    placeholder="请选择省/市/区"
                    :options="cityList"
                    :props="{ value: 'v', label: 'v', children: 'c' }"
                    filterable
                    clearable
                  >
                  </el-cascader>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="牌号">
                  <el-input v-model="item.brand"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="规格">
                  <el-input v-model="item.specification"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="数量">
                  <el-input v-model="item.amount"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="件数">
                  <el-input v-model="item.units"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div v-if="deliveryForm.goodsInfo.length > 1 && !signData">
            <el-button
              @click="del(index)"
              type="text-danger"
              icon="el-icon-delete">删除</el-button>
          </div>
        </div>
        <div class="text-right">
          <el-button
            v-if="!signData"
            type="text"
            icon="el-icon-plus"
            @click="add"
          >增加</el-button>
        </div>
      </div>
      <div class="sign-form-title m-t-20">
        <b>签收信息</b>
      </div>
      <el-row
        class="m-t-10">
        <el-col :span="8">
          <el-form-item label="签收时间">
            <el-date-picker
              v-model="deliveryForm.signInfo.signTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="签收人">
            <el-input v-model="deliveryForm.signInfo.signPerson"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8"></el-col>
      </el-row>
      <div class="sign-form-title m-t-20">
        <b>附件信息</b>
      </div>
      <el-form-item
        class="m-t-10"
        label="附件材料">
        <el-upload
          :class="{'hideUpload': 1 <= deliveryForm.fileInfo.files.length }"
          ref="uploadPic"
          :auto-upload="false"
          accept="image/gif, image/jpeg, image/png"
          action=" "
          :limit="1"
          :file-list="deliveryForm.fileInfo.files"
          list-type="picture-card">
          <i slot="trigger" class="el-icon-plus">
          </i>
        </el-upload>
      </el-form-item>
    </el-form>
    <div class="text-center" v-if="signData">
      <el-button
        @click="close"
        class="m-l-20"
        type="warning"
        >关闭</el-button>
    </div>
    <div class="text-center" v-else>
      <el-button
        @click="submit"
        type="primary"
        >确认提交</el-button>
      <el-button
        @click="close"
        class="m-l-20"
        type="warning"
        >取消</el-button>
    </div>
  </div>
</template>

<script>
import { cityList } from '@/common/js/city.js'
export default {
  props: {
    orderData: {
      type: Object,
      default: function () {
        return {}
      }
    },
    signData: {
      type: Object
    }
  },
  data(){
    return {
      cityList:cityList,
      goodsList: [
        {
          goodsName: '钢铁001', // 商品名称
          originPlace: '曲江区', // 产地
          brand: '0012', // 牌号
          specification: 'LO215', // 规格
        },
        {
          goodsName: '钢铁002', // 商品名称
          originPlace: '曲江区', // 产地
          brand: '0013', // 牌号
          specification: 'LO216', // 规格
        }
      ],
      deliveryForm: {
        goodsInfo: [
          {
            goodsName: '', // 商品名称
            originPlace: '', // 产地
            brand: '', // 牌号
            specification: '', // 规格
            amount: '', // 数量
            units: '', // 件数
          },
          {
            goodsName: '', // 商品名称
            originPlace: '', // 产地
            brand: '', // 牌号
            specification: '', // 规格
            amount: '', // 数量
            units: '', // 件数
          }
        ],
        signInfo: {
          signTime: '', // 签收时间
          signPerson: '', // 签收人
        },
        fileInfo: {
          files: []
        }
      }
    }
  },
  created(){
    console.log('created')
    if (this.signData) {
      this.deliveryForm = {
        goodsInfo: [
          {
            goodsName: '钢铁001', // 商品名称
            originPlace: '曲江区', // 产地
            brand: '0012', // 牌号
            specification: 'LO215', // 规格
            amount: '2', // 数量
            units: '1', // 件数
          },
          {
            goodsName: '钢铁002', // 商品名称
            originPlace: '曲江区', // 产地
            brand: '0012', // 牌号
            specification: 'LO215', // 规格
            amount: '2', // 数量
            units: '1', // 件数
          }
        ],
        signInfo: {
          signTime: '2022-04-16', // 签收时间
          signPerson: '王五', // 签收人
        },
        fileInfo: {
          files: [
            {
              name: 'https://wjc-bucket.oss-cn-beijing.aliyuncs.com/files/61005729a1534b5893e1a54b83e002cfaa.jpg',
              url: 'https://wjc-bucket.oss-cn-beijing.aliyuncs.com/files/61005729a1534b5893e1a54b83e002cfaa.jpg'
            }
          ]
        }
      }
    }
  },
  mounted(){

  },
  methods: {
    close(){
      this.$emit('close');
    },
    goodsChange(index){
      if (this.deliveryForm.goodsInfo[index].goodsName) {
        this.goodsList.map(item => {
          if (item.goodsName == this.deliveryForm.goodsInfo[index].goodsName) {
            this.deliveryForm.goodsInfo[index].originPlace = item.originPlace;
            this.deliveryForm.goodsInfo[index].brand = item.brand;
            this.deliveryForm.goodsInfo[index].specification = item.specification;
          }
        })
      } else {
        this.deliveryForm.goodsInfo[index].originPlace = '';
        this.deliveryForm.goodsInfo[index].brand = '';
        this.deliveryForm.goodsInfo[index].specification = '';
      }
    },
    add(){
      this.deliveryForm.goodsInfo.push({
        goodsName: '', // 商品名称
        originPlace: '', // 产地
        brand: '', // 牌号
        specification: '', // 规格
        amount: '', // 数量
        units: '', // 件数
      });
    },
    del(index){
      this.deliveryForm.goodsInfo.splice(index, 1);
    },
    submit(){
      this.close();
    }
  }
}
</script>

<style lang="scss">
.sign-form{
  .sign-form-title{
    border-bottom: 1px solid #ccc;
  }
}
</style>